/*
* Copyright (c) 2009, Active Web Solutions Limited
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3- The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY Active Web Solutions Limited ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Active Web Solutions Limited BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.aws.jazure;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.log4j.Logger;

public class Sample {
	
	private static void runBlobSamples() throws Exception{
		
		BlobStorage blobStorage = new BlobStorage();
		blobStorage.listContainers();		
		BlobContainer container = blobStorage.getBlobContainer("test");		
		BlobProperties blob = new BlobProperties("Jazure");		
		BlobContents blobContents = new BlobContents(new FileInputStream(new File("blobs/jazure")));		
		boolean result = container.putBlobImpl(blob, blobContents.asInputStream(), true);		
		Logger.getRootLogger().info(result);		
		List blobs =container.listBlobs("", false);	
		for(Object o:blobs){
			if(o instanceof BlobProperties){
				Logger.getRootLogger().info(((BlobProperties)o).getName());
			}
			if(o instanceof String){
				Logger.getRootLogger().info(o);
			}
		}		
		container.getBlobImpl("Jazure", new FileOutputStream(new File("blobs/downloaded/Jazure")));
		
//		boolean res=container.deleteBlob("Jazure");
		
//		Logger.getRootLogger().info(res);
	}
	
	/**
	 * @throws Exception
	 */
	/**
	 * @throws Exception
	 */
	private static void runQueueSamples() throws Exception{

		String endPoint = "http://127.0.0.1:10001/devstoreaccount1";
		String accountName = "devstoreaccount1";
		String accountSharedKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";

		QueueStorage queueStorage = new QueueStorage(endPoint,accountName, accountSharedKey);
		List<Queue> queues = queueStorage.listQueues();
		
		Logger.getRootLogger().info(queues.size());
		
		for (Queue queue : queues) {
			Logger.getRootLogger().info(queue.getQueueName());
		}

		Queue q = queueStorage.getQueue("foo");
		
		q.create();		
		
		q.putMessage(new QueueMessage("Hello World 2"));
		
		// add some meta data to the queue.
		QueueProperties someProperties = new QueueProperties();
		someProperties.getMetaData().put("queueCountry", "UK");
		someProperties.getMetaData().put("queueCity", "Colchester");
		Logger.getRootLogger().info(q.setProperties(someProperties));
		
			
		QueueProperties properties =  q.getProperties();
		Logger.getRootLogger().info(properties); 
		
		List<QueueMessage> messages =  q.getMessages(5);
		for(QueueMessage qmsg:messages){
			Logger.getRootLogger().info(qmsg);
		}
		
		//q.delete();
	}
	
	private static void runTableSamples() throws Exception{
		
		// initialising the table storage
		String endPoint = "http://dyaastorage.table.core.windows.net";
		String accountName = "dyaastorage";
		String accountSharedKey = "y8o+ZnIEeBmjnxkK4b+jy45h1HZ9/JeliJRRrbd07BpFbeJFVu1hp5aEvJcrasPu942DmVAGhIH9kSWvJRj4iw==";
				
		TableStorage tableStorage = new TableStorage(endPoint, accountName, accountSharedKey);
		
		// listing the tables in the table storage
		List<Table> tables = tableStorage.listTables();
		
		for(Table table:tables){
			Logger.getRootLogger().info(table.getTableName());
		}	
		
		Table myTable = tableStorage.getTable("myTable");
		
		myTable.clear();	

		// Create a new entity using a PK and a row key.
		TableStorageEntity entity = new TableStorageEntity("11","xxx");
//		
		entity.getProperties().put("employeeName",new TableStorageProperty(TableStorageProperty.EdmString,"Martin"));
		entity.getProperties().put("employeeSalary",new TableStorageProperty(TableStorageProperty.EdmInt32,new Integer(35000)));
		
		// Inserting a new entity
		Logger.getRootLogger().info(myTable.insertEntity(entity));
		
		
		// Get an entity (pk-rk)
		TableStorageEntity ent = myTable.getEntity("11", "xxx");
		Logger.getRootLogger().info(ent);
		
		// Updating an existing entity
		entity.getProperties().put("employeeSalary",new TableStorageProperty(TableStorageProperty.EdmInt32,new Integer(37000)));
		Logger.getRootLogger().info(myTable.updateEntity(entity));
		
		ent = myTable.getEntity("11", "xxx");
		Logger.getRootLogger().info(ent);
		
		
		// deleting an entity
//		Logger.getRootLogger().info( myTable.delete(ent) );
		
		myTable.insertEntity(new TableStorageEntity("11","aaa"));
		myTable.insertEntity(new TableStorageEntity("11","bbb"));
		myTable.insertEntity(new TableStorageEntity("22","aaa"));		
		
		// Query entities as per http://msdn.microsoft.com/en-us/library/dd894031.aspx
		List<TableStorageEntity> entites =  myTable.queryEntities("PartitionKey%20eq%20'11'");		
		
		Logger.getRootLogger().info("The query returned "+entites.size()+ " entities");
		for(TableStorageEntity e: entites){
			Logger.getRootLogger().info(e);
		}
		
	}
	
	public static void main(String[] args) throws Exception {	
		runBlobSamples();
		runQueueSamples();
		runTableSamples();
	}
}
